<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body></body>

  <script>
    // function converter(obj) {
    //   // let newObj = {}
    //   // for (let key in obj) {
    //   //   let reg = /_([a-zA-Z])/g
    //   //   let result = key.replace(reg, function (a, b) {
    //   //     return b.toUpperCase()
    //   //   })
    //   //   newObj[result] = obj[key]
    //   // }
    //   // return newObj
    //   let newObj = {}
    //   for (let key in obj) {
    //     let reg = /_([a-zA-Z])/g
    //     let result = key.replace(reg, function (a, b) {
    //       return b.toUpperCase()
    //     }) // 将下划线后面的字母转换为大写
    //     newObj[result] = obj[key]
    //   }
    //   return newObj
    // }
    // console.log(converter({ a_bc_def_aaa: 1111 })) // {aBcDefAaa: 1111}
    // 数组拍平函数
    // function flatten(arr) {
    //   let newArr = []
    //   for (let i = 0; i < arr.length; i++) {
    //     if (Array.isArray(arr[i])) {
    //       newArr = newArr.concat(flatten(arr[i]))
    //     } else {
    //       newArr.push(arr[i])
    //     }
    //   }
    //   return newArr
    // }
    // console.log(flatten([1, [2, [3, [4]]]])) // [1, 2, 3, 4]

    function arrayToTree(arr) {
      const idMap = new Map()
      const result = []

      // 初始化 Map
      arr.forEach((item) => {
        idMap.set(item.id, { ...item, children: [] })
      })

      // 构建树
      arr.forEach((item) => {
        const parent = idMap.get(item.parentId)
        if (parent) {
          parent.children.push(idMap.get(item.id))
        } else {
          result.push(idMap.get(item.id))
        }
      })

      return result
    }

    console.log(JSON.stringify(arrayToTree(arr), null, 2))
    const arr = [
      { id: 1, name: 'Node 1', parentId: null },
      { id: 2, name: 'Node 2', parentId: 1 },
      { id: 3, name: 'Node 3', parentId: 1 },
      { id: 4, name: 'Node 4', parentId: 2 },
      { id: 5, name: 'Node 5', parentId: 2 },
    ]
    console.log(arrayToTree(arr))
    console.log(JSON.stringify(arrayToTree(arr), null, 2))


    // 倒计时 useHooks 实现
    function useCountdown(initialTime) {
      // 状态管理
      let time = initialTime
      let isRunning = false
      let intervalId = null
      let callbacks = {
        onTick: null,
        onComplete: null
      }

      // 开始倒计时
      const start = () => {
        if (isRunning) return
        
        isRunning = true
        intervalId = setInterval(() => {
          time--
          
          // 触发每秒回调
          if (callbacks.onTick) {
            callbacks.onTick(time)
          }
          
          // 倒计时结束
          if (time <= 0) {
            pause()
            if (callbacks.onComplete) {
              callbacks.onComplete()
            }
          }
        }, 1000)
      }

      // 暂停倒计时
      const pause = () => {
        if (intervalId) {
          clearInterval(intervalId)
          intervalId = null
        }
        isRunning = false
      }

      // 重置倒计时
      const reset = (newTime) => {
        pause()
        time = newTime || initialTime
        if (callbacks.onTick) {
          callbacks.onTick(time)
        }
      }

      // 设置回调函数
      const onTick = (callback) => {
        callbacks.onTick = callback
      }

      const onComplete = (callback) => {
        callbacks.onComplete = callback
      }

      // 获取当前时间
      const getTime = () => time

      // 获取运行状态
      const getIsRunning = () => isRunning

      return {
        start,
        pause,
        reset,
        onTick,
        onComplete,
        getTime,
        getIsRunning
      }
    }

    // 使用示例
    const countdown = useCountdown(10)
    
    // 设置每秒执行的回调
    countdown.onTick((time) => {
      console.log(`剩余时间: ${time}秒`)
    })
    
    // 设置倒计时完成的回调
    countdown.onComplete(() => {
      console.log('倒计时结束！')
    })
    
    // 开始倒计时
    countdown.start()
    
    // 可以通过以下方法控制倒计时：
    // countdown.pause()    // 暂停
    // countdown.reset()    // 重置到初始时间
    // countdown.reset(20)  // 重置到新的时间
  </script>
</html>
